import axios from "axios"
import router from "@/router";

axios.defaults.timeout=20000;
axios.defaults.withCredentials=true;
// axios.defaults.headers.post["Content-Type"]="application/x-www-form-urlencoded;charset=UTF-8";
axios.defaults.baseURL="http://localhost:9999"

axios.interceptors.response.use(
    (response)=> {
        if (response.status === 200)
        {
            return Promise.resolve(response);
        }else{
            return Promise.reject(response);
        }
    },
    (error) =>{
        if (error.response.status){
            switch(error.response.status){
                case 401:{
                    router.replace({
                        path:"/",
                        query:{}
                    });
                    break;
                }

                case 403 :{
                    setTimeout(()=>{
                        router.replace({
                            path:"/",
                            query:{}
                        })
                    },1000)
                    break;
                }

                case 404:
                    break;
            }
            return Promise.reject(error.response)
        }
    }
);



export function get(url: string,parms?:object){
    return new Promise((resolve,reject) => {
        axios.get(url,parms).then(
            (response) =>resolve(response.data),
            (error)=>reject(error)
        )
    })
};

export function post(url: string,data: any,params?:object){
    return new Promise((resolve,reject)=>{
        axios.post(url,data,params).then(
            (response) =>resolve(response.data),
            (error)=>reject(error)
        );
    });
};